package com.cxy.ch1;

import com.cxy.tools.SleepTools;

/**
 * 
 * @ClassName: UseJoin
 * @Description: TODO(演示Join（）方法的使用)
 * @author 菜徐鸭
 * @date 2020年4月12日
 *
 */
public class UseJoin {

	static class Goddess implements Runnable {
		private Thread thread;

		public Goddess(Thread thread) {
			this.thread = thread;
		}

		public Goddess() {
		}

		public void run() {
			System.out.println("女神开始排队打饭.....");
			try {
				if (thread != null)
					thread.join();
			} catch (InterruptedException e) {
			}
			SleepTools.second(2);// 休眠2秒
			System.out.println(Thread.currentThread().getName() + " 女神打饭完成");
		}
	}

	static class GoddessBoyfriend implements Runnable {

		public void run() {
			SleepTools.second(2);// 休眠2秒
			System.out.println("女神男朋友开始排队打饭.....");
			System.out.println(Thread.currentThread().getName() + " 女神男朋友打饭完成");
		}
	}

	/**
	 * 通过join 可以让多个线程以线性的方式执行
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		Thread dog = Thread.currentThread();
		GoddessBoyfriend goddessBoyfriend = new GoddessBoyfriend();
		Thread gbf = new Thread(goddessBoyfriend);
		Goddess goddess = new Goddess(gbf);
		// Goddess goddess = new Goddess();
		Thread g = new Thread(goddess);
		g.start();
		gbf.start();
		System.out.println("舔狗开始排队打饭.....");
		// g.join();
		SleepTools.second(2);// 让主线程休眠2秒
		System.out.println(Thread.currentThread().getName() + " 舔狗打饭完成.");
	}
}
